package element.synt;

import ast.AstItem;
import ast.Expression;
import element.base.Synt;

public class LOrExp extends Synt {
    @Override
    public AstItem toAst() {
        if (getNodes().size() == 1) {
            LAndExp lAndExp = (LAndExp) getNodes().getFirst();
            return lAndExp.toAst();
        } else {
            LOrExp lOrExp = (LOrExp) getNodes().getFirst();
            LAndExp lAndExp = (LAndExp) getNodes().getLast();

            Expression expression = new Expression();
            expression.operator = Expression.OperatorType.OP_OR;
            expression.operands.add(lOrExp.toAst());
            expression.operands.add(lAndExp.toAst());

            for (AstItem unary: ((Expression) lOrExp.toAst()).allUnaryExp) {
                expression.allUnaryExp.add(unary);
            }
            for (AstItem unary: ((Expression) lAndExp.toAst()).allUnaryExp) {
                expression.allUnaryExp.add(unary);
            }

            return expression;
        }
    }
}
